﻿using System;
using System.Diagnostics;
using ExcelLibrary.SpreadSheet;
using HappyBa.Core.Data;

namespace HappyBa.Core.Reporting
{
    static class ReportExtensions
    {
        public static void SaveResults(this Workbook workbook, string fileName, int lineNo, Worksheet worksheet)
        {
            //workaround to fix save
            for (; lineNo < 100; lineNo++)
            {
                worksheet.Cells[lineNo, 1] = new Cell("");
            }
            worksheet.Cells.ColumnWidth[0, 0] = 2000;
            worksheet.Cells.ColumnWidth[1, 1] = 5000;
            worksheet.Cells.ColumnWidth[2, 2] = 30000;
            workbook.Worksheets.Add(worksheet);
            workbook.Save(fileName);
        }

        public static void Build(this IReportFilePathGetter reportFilePathGetter, Action<string> action)
        {
            string fileName;
            if (reportFilePathGetter.Get(out fileName))
            {
                action(fileName);
                using (Process.Start(fileName))
                {
                }
            }
        }

        public static Cell BuildCell<T>(this Worksheet worksheet, T value)
        {
            return new Cell(value == null ? string.Empty : value.ToString());
        }


        public static Cell BuildCell(this Worksheet worksheet, User u)
        {
            return new Cell(u==null?string.Empty:u.ToString());
        }

        public static Cell BuildCell(this Worksheet worksheet, UIEntityBase e)
        {
            return new Cell(e == null ? string.Empty : e.EntityType.Name + ":" + e.Id);
        }

    }
}
